package com.just.login;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class TestIO {
    public void doRead() {
        try (BufferedReader reader =
                     new BufferedReader(
                     new InputStreamReader(
                     Objects.requireNonNull(
                     getClass().getClassLoader().getResourceAsStream("database/database.txt"))))){
            while (true) {
                if (reader.ready()) {
                    String res = reader.readLine();
                    System.out.println(res);
                }else{
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void doWrite() {
        String pathname = this.getClass().getClassLoader().getResource("database/database.txt").getPath();
        System.out.println(pathname);
        try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathname, true)))) {
            out.newLine();
            out.write("wangwu, 123456");
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        TestIO tio = new TestIO();
        tio.doWrite();
        tio.doRead();
    }

}
